package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.common.constant.BusinessStatusConstances;
import com.qk.common.constant.ClueStatusConstances;
import com.qk.common.constant.ClueTrackRecordType;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.common.util.CurrentUserContextHolders;
import com.qk.domian.clue.ClueDO;
import com.qk.dto.clue.ClueDTO;
import com.qk.dto.clue.ClueListDTO;
import com.qk.dto.clue.UpdateClueInfoDTO;
import com.qk.entity.Business;
import com.qk.entity.Clue;
import com.qk.entity.ClueTrackRecord;
import com.qk.management.mapper.BusinessMapper;
import com.qk.management.mapper.ClueMapper;
import com.qk.management.mapper.ClueTrackRecordMapper;
import com.qk.management.mapper.UserMapper;
import com.qk.management.service.ClueService;
import com.qk.vo.clue.ClueVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @Author:Shuna
 * @Date: 2025/10/13 16:55
 * @Description:
 */
@Service
@Slf4j
public class ClueServiceImpl extends ServiceImpl<ClueMapper, Clue> implements ClueService {

    @Autowired
    public UserMapper userMapper;
    @Autowired
    private ClueTrackRecordMapper clueTrackRecordMapper;
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private BusinessMapper businessMapper;

    public ClueServiceImpl(UserMapper userMapper, ClueTrackRecordMapper clueTrackRecordMapper) {
        this.userMapper = userMapper;
        this.clueTrackRecordMapper = clueTrackRecordMapper;
    }

    private PageResult<ClueDO> clueListMp(ClueListDTO dto) {
        Page pages = new Page(dto.getPage(), dto.getPageSize());
        Page<ClueDO> page = this.baseMapper.selectByPageMp(pages, dto);
        return PageResult.
                <ClueDO>builder()
                .total(Long.valueOf(page.getTotal()).intValue())
                .rows(page.getRecords())
                .build();
    }


    @Override
    public void addClue(ClueDTO dto) {

        if (ObjectUtil.isEmpty(dto.getName()) || ObjectUtil.isEmpty(dto.getChannel())) {
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }

        Clue clue = BeanUtil.copyProperties(dto, Clue.class);
        clue.setCreateTime(LocalDateTime.now());
        clue.setUpdateTime(LocalDateTime.now());
        clue.setStatus(ClueStatusConstances.WAIT_ALLOCATION);
        this.save(clue);
    }

    @Override
    public PageResult<ClueDO> clueList(ClueListDTO dto) {
//        PageHelper.startPage(dto.getPage(), dto.getPageSize());
//        List<ClueDO> list = this.baseMapper.selectByPage(dto);
//        Page<ClueDO> page = (Page<ClueDO>) list;
//        return PageResult.
//                <ClueDO>builder()
//                .total(Long.valueOf(page.getTotal()).intValue())
//                .rows(page.getResult())
//                .build();
        return this.clueListMp(dto);
    }

    @Override
    public ClueVO getClueInfoById(Integer id) {
//        Clue clue = this.baseMapper.selectById(id);
//
//        Integer userId = clue.getUserId();
//
//        String assignName = userMapper.selectById(userId).getName();
//
//        List<ClueTrackRecord> clueTrackRecords = clueTrackRecordMapper.selectList(Wrappers.lambdaQuery(ClueTrackRecord.class)
//                .in(ClueTrackRecord::getClueId, id));
//
//        ClueVO clueVO = BeanUtil.copyProperties(clue, ClueVO.class);
//
//        List<ClueTrackRecordVO> list = clueTrackRecords.stream().map(record -> {
//            ClueTrackRecordVO vo = BeanUtil.copyProperties(record, ClueTrackRecordVO.class);
//            vo.setAssignName(assignName);
//            return vo;
//        }).toList();
//
//        clueVO.setTrackRecords(list);
//        return clueVO;
        return this.baseMapper.selectClueInfoById(id);
    }

    @Transactional
    @Override
    public void updateClueInfoById(UpdateClueInfoDTO dto) {
        Clue clue = BeanUtil.copyProperties(dto, Clue.class);
        clue.setUpdateTime(LocalDateTime.now());
        clue.setStatus(ClueStatusConstances.FOLLOW_UP);
        clueMapper.updateById(clue);
        ClueTrackRecord clueTrackRecord = BeanUtil.copyProperties(dto, ClueTrackRecord.class);
        clueTrackRecord.setCreateTime(LocalDateTime.now());
        clueTrackRecord.setClueId(dto.getId());
        clueTrackRecord.setId(null);
        clueTrackRecord.setUserId(CurrentUserContextHolders.get());
        clueTrackRecord.setType(ClueTrackRecordType.NORMAL);
        clueTrackRecordMapper.insert(clueTrackRecord);
    }

    @Transactional
    @Override
    public void toBusiness(Integer id) {
        Clue clue = this.baseMapper.selectById(id);
        clue.setStatus(ClueStatusConstances.CONVERT_TO_BUSINESS);
        clue.setUpdateTime(LocalDateTime.now());
        this.baseMapper.updateById(clue);
        Business business = BeanUtil.copyProperties(clue, Business.class);
        business.setId(null);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        business.setNextTime(null);
        business.setUserId(null);
        business.setStatus(BusinessStatusConstances.WAIT_ALLOCATION);
        business.setClueId(id);
        this.businessMapper.insert(business);
    }
}
